﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.LichHocLopTinChiService;
using eTMS.Controllers.LopTinChiService;
using eTMS.Controllers.SinhVienLopTinChiService;

namespace eTMS.Controllers
{
    public class LopTinChiController : LopTinChiControllerBase
    {
        public LopTinChiController()
            : base()
        {
        }
        private DataTable _TableThoiKhoaBieu;
        public DataTable TableThoiKhoaBieu
        {
            get
            {
                return _TableThoiKhoaBieu;
            }
            set
            {
                _TableThoiKhoaBieu = value;
            }
        }

        public void UpdateStatus(byte Status, List<DataRow> lsRow)
        {
            foreach (DataRow row in lsRow)
            {
                dao.UpdateTrangThai(Status, ConvertInt(row["LopTinChi_ID"]));
            }
            AddTenTrangThai();
        }
        public void LoadAllDSLopTinChiByHocKyNamHoc(int HocKy_ID, int NamHoc_ID, List<int> ListMonHoc_ID)
        {
            if (ListMonHoc_ID.Count > 0)
            {
                Table = dao.SelectAllDSFilter("NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString() + " AND MonHoc_ID IN(" + string.Join(",", ListMonHoc_ID.ConvertAll<string>(Convert.ToString).ToArray()) + ")").Tables[0];
            }
            else
            {
                Table = dao.SelectAllDSFilter("NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString()).Tables[0];
            }
            AddTenTrangThai();
            AddIndexToTable();
        }
        public void LoadAllDSLopTinChiWithSoSinhVienByHocKyNamHoc(int HocKy_ID, int NamHoc_ID, List<int> ListMonHoc_ID)
        {
            SinhVienLopTinChiService.SinhVienLopTinChiServiceClient w = new SinhVienLopTinChiServiceClient();
            Table = w.SelectAllCountSinhVienDKWithLopTinChiByListMonHoc(NamHoc_ID, HocKy_ID, ListMonHoc_ID.ToArray()).Tables[0];
            Table.Columns.Add("SoTSVDK", typeof(int));
            foreach (DataRow row in Table.Rows)
            {
               row["SoTSVDK"] = ConvertInt(row["SoSVMDK"]) + ConvertInt(row["SoSVDDK"]) + ConvertInt(row["SoSVHDK"]);
            }
            AddTenTrangThai();
            AddIndexToTable();
        }
        public void LoadAllDSLopTinChiByHocKyNamHocTrangThai(int HocKy_ID, int NamHoc_ID, int TrangThai, List<int> ListMonHoc_ID)
        {
            if (ListMonHoc_ID.Count > 0)
            {
                Table = dao.SelectAllDSFilter("NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString() + " AND MonHoc_ID IN(" + string.Join(",", ListMonHoc_ID.ConvertAll<string>(Convert.ToString).ToArray()) + ") AND TrangThai=" + TrangThai.ToString()).Tables[0];
            }
            else
            {
                Table = dao.SelectAllDSFilter("NamHoc_ID=" + NamHoc_ID.ToString() + " AND HocKy_ID=" + HocKy_ID.ToString() + " AND TrangThai=" + TrangThai.ToString()).Tables[0];
            }
            AddTenTrangThai();
            AddIndexToTable();
        }
        private List<int> GetListLopTinChi_ID()
        {
            List<int> ls = new List<int>();
            foreach (DataRow row in Table.Rows)
            {
                ls.Add(ConvertInt(row["LopTinChi_ID"]));
            }
            return ls;
        }
        public void LoadAllThoiKhoaBieuLopTinChi()
        {

            LoadAllThoiKhoaBieuLopTinChi(GetListLopTinChi_ID());
        }
        public void LoadAllThoiKhoaBieuLopTinChi(List<int> ls)
        {
            if (ls.Count == 0) return;
            LichHocLopTinChiService.LichHocLopTinChiServiceClient w = new LichHocLopTinChiServiceClient();
            _TableThoiKhoaBieu = w.SelectAllDSLichHocByListLop(ls.ToArray()).Tables[0];
            _TableThoiKhoaBieu.DefaultView.Sort = "ThuTuThu";
        }
        public void DeleteLopTinChi(List<DataRow> list)
        {
            foreach (DataRow row in list)
            {
                if (DeleteByID(ConvertInt(row["LopTinChi_ID"])))
                {
                    Table.Rows.Remove(row);
                }
            }
        }
        public void UpdateSoSinhVienToiDa()
        {
            foreach (DataRow row in Table.Rows)
            {
                dao.UpdateSoSVToiDa(ConvertInt(row["SoSVToiDa"]), ConvertInt(row["LopTinChi_ID"]));
            }
        }
        private void AddTenTrangThai()
        {
            if (!Table.Columns.Contains("TenTrangThai"))
                Table.Columns.Add("TenTrangThai");
            foreach (DataRow row in Table.Rows)
            {
                int trangthai = ConvertInt(row["TrangThai"]);
                if (trangthai == eTMS.Commons.Constants.TRANGTHAI_LOPTINCHI_MOITAO)
                    row["TenTrangThai"] = "Mới tạo";
                else if (trangthai == eTMS.Commons.Constants.TRANGTHAI_LOPTINCHI_CHOPHEPDK)
                    row["TenTrangThai"] = "Cho phép ĐK";
                else if (trangthai == eTMS.Commons.Constants.TRANGTHAI_LOPTINCHI_DONGDK)
                    row["TenTrangThai"] = "Đóng ĐK";
            }
        }
        public void AddRangeLop(List<string> lsMaLop, int MonHoc_ID, int NamHoc_ID, int HocKy_ID, int SoSVToiDa)
        {
            List<LopTinChiEntity> lsLop = new List<LopTinChiEntity>();
            foreach (string maLop in lsMaLop)
            {
                ResetObject();
                LopTinChiObject.MaLop = maLop;
                LopTinChiObject.SoSVToiDa = SoSVToiDa;
                LopTinChiObject.MonHoc_ID = MonHoc_ID;
                LopTinChiObject.NamHoc_ID = NamHoc_ID;
                LopTinChiObject.HocKy_ID = HocKy_ID;
                LopTinChiObject.TrangThai = eTMS.Commons.Constants.TRANGTHAI_LOPTINCHI_MOITAO;
                LopTinChiObject.Lop_ID = 0;
                lsLop.Add(LopTinChiObject);
            }
            dao.SaveList(lsLop.ToArray());
        }
        public void UpdateLop()
        {
            List<LopTinChiEntity> lsLop = new List<LopTinChiEntity>();
            foreach (DataRow row in Table.Rows)
            {
                lsLop.Add(MapDataRowToObject(row));
            }
            dao.UpdateList(lsLop.ToArray());
        }
        public int CountSinhVienDangKy(int NamHoc_ID, int HocKy_ID, int MonHoc_ID)
        {
            return dao.CountSinhVienDangKy(NamHoc_ID, HocKy_ID, MonHoc_ID);
        }
    }
}
